<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="company" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item
                      @click.native="hAdd('')"
                    >添加子部门</el-dropdown-item>
                    <el-dropdown-item>编辑部门</el-dropdown-item>
                    <el-dropdown-item>删除部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体 - 放置一个el-tree组件 -->

        <el-tree
          default-expand-all
          :data="data"
          icon-class=" "
          @node-click="handleNodeClick"
        >
          <template slot-scope="{ data, node }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px;width: 100%;"
            >
              <el-col :span="20">
                <svg-icon
                  v-if="data.children.length !== 0"
                  :icon-class="node.expanded ? 'minus' : 'add'"
                />
                <svg-icon v-else icon-class="user1" />
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item
                          @click.native="hAdd(data.id)"
                        >添加子部门</el-dropdown-item>
                        <el-dropdown-item
                          @click.native="hEdit(data.id)"
                        >编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                        >删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      :title="cTitle"
      :visible.sync="showDialog"
      @close="hClose"
    >
      <DeptDialog
        :id="curId"
        ref="refDeptDialog"
        :origin-list="originList"
        :is-edit="isEdit"
        @success="hSuccess"
        @close="showDialog = false"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getDepartments, delDepartments } from '@/api/departments'
import { tranListToTreeData } from '@/utils/index.js'

import DeptDialog from './deptDialog.vue'
export default {
  name: 'Departments',
  components: { DeptDialog },
  data() {
    return {
      isEdit: false, // 是否处于编辑状态
      curId: '', // 当前操作的部门的id
      data: [], // 所有的部门信息
      showDialog: false,
      originList: [] // 给子组件做校验的参考数据
    }
  },
  computed: {
    cTitle() {
      return this.isEdit ? '编辑' : '添加'
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    // 子组件抛出了success事件
    hSuccess() {
      // 1. 关闭弹层
      this.showDialog = false
      // 2. 重新渲染
      this.loadDepartments()
    },
    // 用户点击了添加子部门
    hAdd(id) {
      // 显示弹层
      this.showDialog = true
      // 更新当前操作的id
      this.curId = id

      // 修改状态
      this.isEdit = false
    },
    // 用户点击了删除部门

    hDel(id) {
      // console.log(data)
      // 弹层询问，是否删除
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.doDel(id)
        })
        .catch(() => {
          // 用户取消删除
        })
    },
    async doDel(id) {
      // 1. 删除
      const res = await delDepartments(id)
      console.log('删除的结果是', res)

      // 2. 重发请求
      this.loadDepartments()
    },
    // 用户点击了编辑部门
    hEdit(id) {
      // 修改状态
      this.isEdit = true

      // 更新当前操作的id
      this.curId = id

      // 显示弹层
      this.showDialog = true

      // 调用子组件的方法去发请求获取详情
      // this.$nextTick(() => {
      //   console.log('获取子组件的引用', this.$refs.refDeptDialog)
      //   this.$refs.refDeptDialog.loadDepartmentById()
      // })
    },
    async loadDepartments() {
      const res = await getDepartments()
      console.log(res)
      // 保存

      // 第一个元素是公司的名字，所以去掉
      // 删除数组的第一个元素 shift
      res.data.depts.shift()

      // 加工数据originList，准备给子组件做校验使用
      // res.data.depts = [{id,pid,code,createTime,introduce}]
      this.originList = res.data.depts.map(item => {
        return { id: item.id, pid: item.pid, name: item.name, code: item.code }
      })

      // 调用提前封装好的工具函数来做格式转换
      this.data = tranListToTreeData(res.data.depts)
    },
    handleNodeClick(data) {
      console.log(data)
    },
    hClose() {
      // alert('弹框关闭')
      // this.$refs.refDeptDialog.清空表单校验()
      this.$refs.refDeptDialog.resetForm()
    }
  }
}
</script>
